﻿using System;

namespace Pata.SylkSharp.Core.Bll
{
    /// <summary>
    /// Enumerates the possible fields in a <see cref="IDRecord"/>
    /// </summary>
    public enum IDRecordFieldType
    {
        /// <summary>
        /// file creator, possible creators include :
        /// - MP (Multiplan)
        /// - XL (Excel)
        /// </summary>
        P = 1,
        /// <summary>
        /// If present, file uses ;N style cell protection
        /// If absent, file uses ;P style cell protection
        /// </summary>
        N = 2,
        /// <summary>
        /// If present, NE records are redundant
        /// If absent, NE records are not redundant
        /// </summary>
        E = 3,
        /// <summary>
        /// Unknown field type.
        /// </summary>
        Unknown = 100
    }

    /// <summary>
    /// Object representation of field of a Sylk record of type 'ID'
    /// </summary>
    public class IDRecordField : FieldBase<IDRecordFieldType>
    {
        /// <summary>
        /// Instanciate a new <see cref="IDRecordField"/>
        /// </summary>
        public IDRecordField()
            : base(IDRecordFieldType.Unknown, String.Empty) { }

        /// <summary>
        /// Instanciate a new <see cref="IDRecordField"/>
        /// </summary>
        /// <param name="pType">Type of this field.</param>
        /// <param name="pValue">The value contained by this field.</param>
        public IDRecordField(IDRecordFieldType pType, string pValue)
            : base(pType, pValue) { }
    }

    /// <summary>
    /// Object representation of a Sylk record of type 'ID'.
    /// </summary>
    public class IDRecord : RecordBase<IDRecordField, IDRecordFieldType>
    {
        private static readonly IDRecordFieldType[] _MANDATORY_FIELDS = new IDRecordFieldType[] { IDRecordFieldType.P };

        /// <summary>
        /// Instanciate a new <see cref="IDRecord"/>
        /// </summary>
        public IDRecord(string pLineWithoutId)
            : base(RecordType.ID, pLineWithoutId, IDRecordFieldType.Unknown) { }

        /// <summary>
        /// Get the cell protection style.
        /// </summary>
        /// <returns></returns>
        public CellProtectionStyle GetCellProtectionStyle()
        {
            return this.HaveField(IDRecordFieldType.N) ? CellProtectionStyle.N : CellProtectionStyle.P;
        }

        /// <summary>
        /// Indicates if the <see cref="NERecord"/>s (External Links) are redundant.
        /// </summary>
        /// <returns></returns>
        public bool IsNERedundant()
        {
            return this.HaveField(IDRecordFieldType.E);
        }

        /// <summary>
        /// Get mandatory fields for this record.
        /// </summary>
        /// <returns></returns>
        public override System.Collections.Generic.IEnumerable<IDRecordFieldType> GetMandatoryFields()
        {
            return _MANDATORY_FIELDS;
        }

        /// <summary>
        /// Get the default field type.
        /// </summary>
        /// <returns></returns>
        public override IDRecordFieldType GetUnknownFieldType()
        {
            return IDRecordFieldType.Unknown;
        }
    }
}
